package com.ihr360.payroll.controller.salarytask.validator;

import com.google.common.collect.Lists;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import com.ihr360.payroll.service.salarytask.SalaryTaskService;
import com.ihr360.payroll.service.salarytask.impl.SalaryTaskServiceImpl;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-16 10:31:39.
 */
@Component
@SpreadSheetValid(code = "SalaryTask")
public class SalaryTaskImportValidator implements SpreadSheetValidator {

    private static final String PARAM_ID = "id";

    @Autowired
    private SalaryTaskService salaryTaskService;

    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        Ihr360Session session = Ihr360SessionContextHolder.getSession();
        if (session == null || session.getUserInfo() == null
                || session.getUserInfo().getCompanyId() == null) {
            ValidationUtils.reject(null, "【薪资数据导入】请求参数无效");
        }

        Map<String, Object> extraParams = request.getExtraParams();
        if (!hasCorrectIdParam(extraParams)) {
            ValidationUtils.reject(null, "【薪资数据导入】请求参数无效");
        }
        return salaryTaskService.validateExcelData(
                Long.valueOf(extraParams.get(PARAM_ID).toString()), request);
    }

    private boolean hasCorrectIdParam(Map<String, Object> extraParams) {
        if (CollectionUtils.isEmpty(extraParams)) {
            return false;
        }
        Object idObject = extraParams.get(PARAM_ID);
        if (idObject == null) {
            return false;
        }

        try {
            Long.parseLong(idObject.toString());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        CommonSpecification commonSpecification = CommonSpecification.createCommonSpecification(true);

        List<List<String>> requiredHeaders = Lists.newArrayList();
        ArrayList<String> staffNameColumn = Lists.newArrayList(SalaryTaskServiceImpl.EXCEL_FIELD_NAME);
        requiredHeaders.add(staffNameColumn);
        ArrayList<String> mobileNoColumn = Lists.newArrayList(SalaryTaskServiceImpl.EXCEL_FIELD_MOBILE_NO);
        requiredHeaders.add(mobileNoColumn);

        commonSpecification.setHeaderColumnJudge(requiredHeaders);
        commonSpecification.setStaffNameAlias(staffNameColumn);
        commonSpecification.setMobileNoAlias(mobileNoColumn);
        spreadSheetSpecification.setCommonSpecification(commonSpecification);

        return spreadSheetSpecification;
    }
}
